package org.oneblog.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.oneblog.controller.SessionController;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class ManagerInterceptor extends HandlerInterceptorAdapter  {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		
		//如果是 article/manager 则正常访问，否则验证token是否有效
		String requestURI = request.getRequestURI();
		if(requestURI.endsWith("article/manager") || requestURI.endsWith("article/login")) {

			return super.preHandle(request, response, handler);
		}

		String sessionToken = SessionController.getSessionToken(request, response);
        System.out.println("interceptor sessionToken : " + sessionToken);
        
        if(StringUtils.isEmpty(sessionToken)) {//token无效
        	//http://localhost:8080/oneblog-web/redirect?url=/article/manager
        	request.setAttribute("mm", "mssssssss");
        	response.sendRedirect("../redirect?url=article/manager");
        	return false;
        }
        
		return super.preHandle(request, response, handler);
	}
}
